//
//  HomeView.m
//  Example
//
//  Created by Rich on 16/4/20.
//  Copyright © 2016年 Rich. All rights reserved.
//

#import "HomeView.h"
#import "RefreshTableView.h"
#import "ArrayDataSource.h"
#import "HomeCell+Config.h"
#import "HomeHeaderView.h"

@interface HomeView()<UITableViewDelegate,RefreshTableViewDelegate,HomeHeaderViewDelegate>

@property(nonatomic,strong) RefreshTableView *tableView;
@property(nonatomic,strong) HomeHeaderView *headerView;
@property(nonatomic,strong) ArrayDataSource *dataSource;
@property(nonatomic,strong) NSArray* dataArr;


@end

static NSString *identify = @"cell";

@implementation HomeView


-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        self.backgroundColor = BG_COLOR;
        [self createTableView];
        [self createHeaderView];
        
    }
    return self;
}

#pragma mark -

- (void)createTableView
{
    if (self.tableView == nil)
    {
        CGFloat originY = 0.0f;
        _tableView = [[RefreshTableView alloc] initWithFrame:CGRectMake(0.0f, originY, self.bounds.size.width, self.bounds.size.height-originY) style:UITableViewStyleGrouped];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.delegate = self;
        self.tableView.refreshDelegate = self;
        //self.tableView.dataSource = self;
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        
        
        /*
         self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
         TableViewCellConfigureBlock configureCell = ^(ProjectCell *cell, id item) {
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.delegate = self;
         cell.themeVO = self.themeVO;
         [cell configure:item];
         };
         self.dataSource = [[ArrayDataSource alloc] initWithCellIdentifier:identify andConfigureCellBlock:configureCell];
         self.dataSource.numberOfSections = 1;
         self.tableView.dataSource = self.dataSource;
         [self.tableView registerClass:[ProjectCell class] forCellReuseIdentifier:identify];
         
         [self.tableView setupRefresh];
         */
        
        //self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        TableViewCellConfigureBlock configureCell = ^(HomeCell *cell, id item) {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.homeCellDelegate = self;
            [cell configure:item];

        };
        self.dataSource = [[ArrayDataSource alloc] initWithCellIdentifier:identify andConfigureCellBlock:configureCell];
        self.dataSource.numberOfSections = 1;
        self.tableView.dataSource = self.dataSource;
        
        [self.tableView registerClass:[HomeCell class] forCellReuseIdentifier:identify];
        
        [self.tableView setupRefresh];
        
    }
}

- (void)createHeaderView
{
    if (self.headerView == nil) {
        CGFloat height = 236.0f + self.width*(11.0f/32.0f)-130.0f;
        CGRect rect = CGRectMake(0.0f, 0.0f, self.width, height);
        _headerView = [[HomeHeaderView alloc] initWithFrame:rect];
        self.headerView.homeHeaderViewDelegate = self;
        self.tableView.tableHeaderView = self.headerView;
    }
}

@end
